<?php

namespace tmcore\services\addons;

use app\common\model\addons\Addons;

class ModelService extends BaseService
{
    protected Addons $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new Addons();
    }

    /**
     * 根据key获取插件信息
     * @param string $key
     * @return array
     */
    public function getInfoByKey(string $key)
    {
        return $this->model->where([['identify', '=', $key]])->findOrEmpty()->toArray();
    }

    /**
     * 添加插件信息
     * @param array $params
     * @return bool|int
     */
    public function addAddonInfo(array $params)
    {
        return $this->model->add([
            'name' => $params['name'],
            'identify' => $params['identify'],
            'thumb' => $params['thumb'],
            'is_local' => empty($params['is_local'])?0:$params['is_local'],
            'status' => 1,
            'version' => $params['version'],
        ], true);
    }

    /**
     * 设置插件信息
     * @param array $params
     * @return bool
     */
    public function setAddonInfo(array $params)
    {
        return $this->model->edit([
            'name' => $params['name'],
            'thumb' => $params['thumb'],
            'version' => $params['version'],
        ], $params['identify'], 'identify');
    }

    /**
     * 删除插件信息
     * @param string $key
     * @return bool
     */
    public function delAddonInfo(string $key)
    {
        return $this->model->where([['identify', '=', $key]])->delete();
    }
}